using NL;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

namespace Miya.Hellow
{

    public class FileRequester
    {
        public string Url;
        // Start is called before the first frame update

        public bool _Ready = false;

        public string Result;

        public string Error;

        public FileRequester(string url)
        {
            Url = url;
        }

        public AsyncOperation Request()
        {
            Error = "";
            UnityWebRequest QQ = UnityWebRequest.Get(Url);

            return QQ.SendWebRequest();


        }


        public static FileRequester Request(string url)
        {
            return new FileRequester(url);
        }
    }

    public class TextFileRequest : MonoBehaviour
    {

        public string Url;
        // Start is called before the first frame update

        public bool _Ready = false;

        public string Result;

        public string Error;

        public TextFileRequest SetUrl(string url)
        {
            Url = url; return this;
        }
        IEnumerator Start()
        {
            _Ready = false;

            yield return _FileReader(Url);

            _Ready = true;


            yield return new WaitForSeconds(0.2f);

            Destroy(gameObject);
        }
        IEnumerator _FileReader(string fileurl)
        {
            Error = "";
            UnityWebRequest QQ = null;
            string url = fileurl;
            try
            {
                url = System.Uri.EscapeUriString(fileurl);
                QQ = UnityWebRequest.Get(url);

            }
            catch (Exception ex)
            {
                Error = ex.Message;
                WELCOME.Log($"[TextFileRequest._FileReader]:Error:{ex}  @{url}  ");
            }

            if (QQ == null) yield break;

            yield return QQ?.SendWebRequest();
            while (!QQ.isDone)
                yield return 0;

            if (string.IsNullOrEmpty(QQ.error) && !string.IsNullOrEmpty(QQ.downloadHandler.text))
            {
                Error = "";
                Result = QQ.downloadHandler.text;
                WELCOME.Log($"[TextFileRequest._FileReader]:Url={url}  \n{Result} ");

            }
            else
            {
                Error = QQ.error;
                WELCOME.Log($"[TextFileRequest._FileReader]:Error={QQ.error} @{url}");

                if (!url.StartsWith("file://"))
                {
                    yield return _FileReader($"file://{fileurl}");
                }
            }
        }

        public T ResultAsIni<T>(char linechar = '\n') where T : NLInfo, new()
        {
            T t = new T();
            t.ReadFromString(Result, linechar);
            return t;
        }
        public WaitUntil WaitEnd()
        {
            return new WaitUntil(() => { return _Ready; });
        }

        public static TextFileRequest Request(string url)
        {
            return new GameObject($"FileRequest(url)").AddComponent<TextFileRequest>().SetUrl(url);
        }
    }
}

